//
//  HJTextView.swift
//  Weibo
//
//  Created by 韩佳 on 16/4/25.
//  Copyright © 2016年 ddg. All rights reserved.
//

import UIKit

class HJTextView: UITextView {

    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setupUI()
    }
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        addSubview(placehoderLabel)
        placehoderLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(8)
            make.leading.equalTo(5)
            make.width.equalTo(self).offset(-10)
        }
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "valueChange", name: UITextViewTextDidChangeNotification, object: nil)
    }
    @objc private func valueChange(){
        placehoderLabel.hidden = hasText()
    }
    override var font: UIFont?{
        didSet{
            placehoderLabel.font = font
        }
    }
    var placeholder:String?{
        didSet{
            placehoderLabel.text = placeholder
        }
    }
    //懒加载控件
    private lazy var placehoderLabel:UILabel = {
        let label = UILabel(textColor: UIColor.grayColor(), fontSize: 14)
        label.text = "分享新鲜事..."
        label.numberOfLines = 0
        return label
    
    }()
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
}
